package com.bwda.dsrs.base.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * @author yangjun
 * @date 2019/3/18
 */
public abstract class BaseUIDFactory {

    protected static final int NUM0 = 0;
    protected static final int NUM1 = 1;
    protected static final int NUM2 = 2;
    protected static final int NUM3 = 3;
    protected static final int NUM4 = 4;
    protected static final int NUM5 = 5;
    protected static final int NUM6 = 6;
    protected static final int NUM26 = 26;
    protected static final int NUM52 = 52;
    protected static final int NUM62 = 62;
    protected static final int NUM63 = 63;
    protected static final int NUM25 = 25;
    protected static final int NUM51 = 51;
    protected static final int NUM61 = 61;
    protected static final byte NUM0X0F = 15;
    protected static final byte NUM0X03 = 3;
    protected static final int NUM0XC0 = 192;
    protected static final int NUM0XF0 = 240;
    protected static final int NUM0XFC = 252;
    protected static final byte NUM0X3F = 63;
    protected static final byte NUM0XF = 15;
    protected static final int NUM32 = 32;
    protected static final int NUM16 = 16;
    protected static final int NUM10 = 10;
    protected static final long NUM4294967296L = 4294967296L;
    protected static final int NUM15 = 15;
    protected static final int NUM8 = 8;
    protected static final int NUM255 = 255;
    protected static final int NUM60 = 60;
    protected static final int NUM24 = 24;
    protected static final long NUM30L = 30L;
    protected static final int NUM1000 = 1000;
    protected static final int NUM23 = 23;
    protected static final int NUM59 = 59;
    protected static final int NUM0XFF000000 = -16777216;
    protected static final int NUM0XFF0000 = 16711680;
    protected static final int NUM0XFF00 = 65280;
    protected static final int NUM0XFF = 255;
    protected static final int NUM30 = 30;
    protected static final int NUM9 = 9;
    protected static final int NUM11 = 11;
    protected static final int NUM20 = 20;
    protected static final int NUM50 = 50;
    protected static final int NUM100 = 100;
    protected static final int NUM240 = 240;
    protected static final int NUM1024 = 1024;
    protected static final int NUM200 = 200;
    protected static final int NUM127 = 127;
    protected static final int SECOND_PER_MINUTE = 60;
    protected static final int SECOND_PER_HOUR = 3600;
    protected static final int SECOND_PER_DAY = 86400;
    protected static final int SECOND_PER_WEEK = 604800;
    protected static final String UID_GUID = "GUID";
    protected static final String UID_UUID = "UUID";
    protected static final long EPOCH = System.currentTimeMillis();
    protected static final long JVMHASH = -2147483648L;
    protected static final long MACHINEID = getMachineId();
    protected static final Random M_RANDOM;
    private static MessageDigest md5;
    private boolean isMd5 = false;
    private static final Logger logger = LoggerFactory.getLogger(Md5Utils.class);

    public BaseUIDFactory() {
    }

    public static BaseUIDFactory getDefault() {
        return UUID.getInstance();
    }

    public static BaseUIDFactory getInstance(String uidfactory) throws Exception {
        if (UID_UUID.equalsIgnoreCase(uidfactory)) {
            return UUID.getInstance();
        } else {
            throw new Exception(uidfactory + " Not Found!");
        }
    }

    /**
     * 获得下个uid
     * @return
     */
    public abstract String getNextUid();

    /**
     * 得到uid
     * @return
     */
    public abstract String getUid();

    public boolean isMd5() {
        return this.isMd5;
    }

    public void setMd5(boolean flag) {
        this.isMd5 = flag;
    }

    /**
     * 设置uid
     * @param var1
     * @throws Exception
     */
    public abstract void setUid(String var1) throws Exception;

    /**
     * 打印
     * @return
     */
    public abstract String toPrintableString();

    protected static byte[] toMd5(byte[] bytes) {
        return md5.digest(bytes);
    }

    private static long getMachineId() {
        long i = 0L;

        try {
            InetAddress inetaddress = InetAddress.getLocalHost();
            byte[] abyte0 = inetaddress.getAddress();
            i = (long) toInt(abyte0);
        } catch (Exception var4) {
            logger.error(var4.getMessage(), var4);
        }

        return i;
    }

    private static int toInt(byte[] abyte0) {
        int i = abyte0[0] << 24 & -16777216 | abyte0[1] << 16 & 16711680 | abyte0[2] << 8 & '\uff00' | abyte0[3] & 255;
        return i;
    }

    static {
        M_RANDOM = new Random(EPOCH);

        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException var1) {
//            log.error(var1.getMessage());
        }

    }
}
